package com.salary.utils.reader;

import org.apache.poi.ss.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class SocialInsuranceReader extends DataReader<Map<String, Double>>{
    private static final Logger logger = LoggerFactory.getLogger(SocialInsuranceReader.class);
    @Override
    public Map<String, Double> readData(String socialInsuranceFilePath) {
        Map<String, Double> socialInsuranceData = new HashMap<>();

        try (FileInputStream fis = new FileInputStream(socialInsuranceFilePath);
             Workbook workbook = WorkbookFactory.create(fis)) {

            // 获取第一个工作表
            Sheet sheet = workbook.getSheetAt(0);
            if (sheet == null) {
                logger.error("社保表中未找到工作表");
                return socialInsuranceData;
            }

            logger.info("从社保表的'" + sheet.getSheetName() + "'工作表读取数据");

            // 使用通用方法查找列
            Map<String, Integer> columnIndices = findColumnIndices(sheet, new String[]{"姓名", "社保"});

            // 验证是否找到必要的列
            if (!columnIndices.containsKey("姓名") || !columnIndices.containsKey("社保")) {
                logger.error("社保表中未找到必要的列");
                return socialInsuranceData;
            }

            int nameColIndex = columnIndices.get("姓名");
            int socialInsuranceColIndex = columnIndices.get("社保");

            // 从第二行开始读取数据（跳过表头）
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                if (row == null) continue;

                Cell nameCell = row.getCell(nameColIndex);
                Cell socialInsuranceCell = row.getCell(socialInsuranceColIndex);

                if (nameCell == null) continue;

                String name = getCellValueAsString(nameCell).trim();
                if (name.isEmpty()) continue;

                double socialInsurance = 0.0;
                if (socialInsuranceCell != null) {
                    socialInsurance = getCellValueAsDouble(socialInsuranceCell);

                    // 如果社保金额为0，则尝试直接读取单元格的字符串值并转换
                    if (socialInsurance == 0) {
                        try {
                            String socialInsuranceStr = getCellValueAsString(socialInsuranceCell).trim();
                            if (!socialInsuranceStr.isEmpty()) {
                                // 移除可能的货币符号和千分位分隔符
                                socialInsuranceStr = socialInsuranceStr.replace(",", "").replace("¥", "").replace("￥", "");
                                socialInsurance = Double.parseDouble(socialInsuranceStr);
                            }
                        } catch (NumberFormatException e) {
                            // 忽略解析错误
                        }
                    }
                }

                if (socialInsurance > 0) {
                    socialInsuranceData.put(name, socialInsurance);
                    logger.info("读取到社保数据: " + name + " - " + socialInsurance);
                }
            }

            logger.info("成功从社保表读取了 " + socialInsuranceData.size() + " 条记录");

        } catch (IOException e) {
            logger.error("读取社保表文件失败: " + e.getMessage(), e);
        }

        return socialInsuranceData;
    }
}
